// composables/theme.ts
export const useTheme = () => {
  const isDark = useState<boolean>("darkMode", () => false);

  // 持久化逻辑（网页1方案）
  const initTheme = () => {
    if (process.client) {
      const savedTheme = localStorage.getItem("darkMode");
      isDark.value = savedTheme
        ? JSON.parse(savedTheme)
        : window.matchMedia("(prefers-color-scheme: dark)").matches;
    }
  };

  const toggleTheme = () => {
    isDark.value = !isDark.value;
    document.documentElement.classList.toggle("dark", isDark.value);
    localStorage.setItem("darkMode", JSON.stringify(isDark.value));
  };

  return { isDark, toggleTheme, initTheme };
};
